﻿module Lhz.Page.DataManagement {
    export class AuthorizationDetail extends DetailPage<Lhz.Entity.ServiceCenter.Authorization> {
        private applications: Lhz.Entity.ServiceCenter.Application[];
        private services: Lhz.Entity.ServiceCenter.Service[];
        private $authorizationType: JQuery;
        private $selectApplication: JQuery;
        private $selectService: JQuery;
        private $selectOperation: JQuery;
        constructor() {
            super({
                serviceNo: "authorization",
                mappingOptionsArray: [
                    { propertyName: "AuthorizationType", selector: "[name=AuthorizationType]" },
                    {
                        propertyName: "ObjectGuid",
                        selector: "[name=ObjectGuid]",
                        show: function (ad: AuthorizationDetail, authorization: Lhz.Entity.ServiceCenter.Authorization) {
                            if (authorization.AuthorizationType !== undefined) {
                                ad.$authorizationType.val(authorization.AuthorizationType.toString());
                                ad.$authorizationType.change();
                                var applicationGuid: string;
                                var serviceGuid: string;
                                var operationGuid: string;
                                switch (authorization.AuthorizationType) {
                                    case 0:
                                        applicationGuid = authorization.Application.Guid;
                                        break;
                                    case 1:
                                        applicationGuid = authorization.Service.ApplicationGuid;
                                        serviceGuid = authorization.Service.Guid;
                                        break;
                                    case 2:
                                        applicationGuid = authorization.Operation.Service.ApplicationGuid;
                                        serviceGuid = authorization.Operation.ServiceGuid;
                                        operationGuid = authorization.Operation.Guid;
                                        break;
                                    default:
                                        throw "未知授权类型【" + authorization.AuthorizationType + "】。";
                                }
                                if (authorization.AuthorizationType > -1) {
                                    ad.$selectApplication.val(applicationGuid);
                                    ad.$selectApplication.change();
                                }
                                if (authorization.AuthorizationType > 0) {
                                    ad.$selectService.val(serviceGuid);
                                    ad.$selectService.change();
                                }
                                if (authorization.AuthorizationType > 1) {
                                    ad.$selectOperation.val(operationGuid);
                                }
                            }
                        }
                    }
                ],
                createItem: function (dp) {
                    var authorization = new Lhz.Entity.ServiceCenter.Authorization();
                    authorization.UserGuid = dp.arguments.userGuid;
                    return authorization;
                },
                initiate: function (ad: AuthorizationDetail, callback: (ad: DetailPage<Lhz.Entity.ServiceCenter.Authorization>) => void) {
                    ad.$authorizationType = $("[name=AuthorizationType]");
                    ad.$selectApplication = $("#selectApplication");
                    ad.$selectService = $("#selectService");
                    ad.$selectOperation = $("#selectOperation");
                    $(".authorization-object").hide();
                    ad.$authorizationType.change(ad, function (event) {
                        var ad: AuthorizationDetail = event.data;
                        $(".authorization-object").each(function () {
                            $(this).hide();
                            $(this).removeAttr("required");
                            $(this).removeAttr("name");
                        });
                        $(".authorization-object").hide();
                        ad.buildValidate();
                        var value: string = $(event.target).val();
                        var authorizationType = parseInt(value);
                        var $objectSelect: JQuery;
                        if (value) {
                            ad.$selectApplication.show();
                            if (authorizationType == 0) {
                                $objectSelect = ad.$selectApplication;
                            }
                        }
                        if (authorizationType > 0) {
                            ad.$selectService.show();
                            if (authorizationType == 1) {
                                $objectSelect = ad.$selectService;
                            }
                        }
                        if (authorizationType > 1) {
                            ad.$selectOperation.show();
                            if (authorizationType == 2) {
                                $objectSelect = ad.$selectOperation;
                            }
                        }
                        if ($objectSelect) {
                            $objectSelect.attr("required", "");
                            $objectSelect.attr("name", "ObjectGuid");
                        }
                    });
                    $("#selectApplication").change(ad, function (event) {
                        var ad: AuthorizationDetail = event.data;
                        var applicationGuid = $(event.target).val();
                        var services: Lhz.Entity.ServiceCenter.Service[] = [];
                        for (var service of ad.services) {
                            if (service.ApplicationGuid == applicationGuid) {
                                services.push(service);
                            }
                        }
                        ad.bindSelect("#selectService", services, "Guid", "Name");
                    });
                    $("#selectService").change(ad, function (event) {
                        var ad: AuthorizationDetail = event.data;
                        var value: string = $(event.target).val();
                        if (value) {
                            var source: Lhz.Entity.ServiceCenter.Service[] = ad.services;
                            var service: Lhz.Entity.ServiceCenter.Service;
                            for (var item of source) {
                                if (item.Guid == value) {
                                    service = item;
                                }
                            }
                            if (item) {
                                ad.bindSelect("#selectOperation", service.Operations, "Guid", "Name");
                            }
                        }
                    });
                    ad.sci.InvokeByInvokeArgument<Lhz.Entity.ServiceCenter.Application[]>({
                        serviceNo: "application",
                        operationNo: "query",
                        arguments: [null],
                        successCallback: function (results, data) {
                            var ad: AuthorizationDetail = data;
                            ad.applications = results.Result;
                            ad.bindSelect("#selectApplication", ad.applications, "Guid", "Name");
                            ad.sci.InvokeByInvokeArgument<Lhz.Entity.ServiceCenter.Service[]>({
                                serviceNo: "service",
                                operationNo: "query",
                                arguments: [null],
                                successCallback: function (results, data) {
                                    var ad: AuthorizationDetail = data;
                                    ad.services = results.Result;
                                    callback(ad);
                                },
                                data: ad
                            });
                        },
                        data: ad
                    });
                }
            })
        }
    }
    $(function () {
        new AuthorizationDetail();
    });
}
